import java.util.ArrayList;
import java.util.Iterator;
import java.util.ListIterator;

public class List {
    public static void main(String[] args) {
        ArrayList<Integer> list = new ArrayList<>();
        list.add(1);
        list.add(2);
        list.add(2,3);
        list.add(3,4);
        list.add(5);
        list.add(6);
        //elist.remove(2);//删除下标2的元素
        list.remove(Integer.valueOf(1));
        System.out.println(list);

        //addAll的测试使用
        ArrayList<Integer> list1 = new ArrayList<>();
        list1.add(7);
        list1.add(8);
        list.addAll(1,list1);//索引1开始插入
        System.out.println(list);
        System.out.println(list.get(1));
        list.set(0,5);
        System.out.println(list);
        System.out.println(list.contains(1));//返回true/false
        System.out.println(list.indexOf(5));
        System.out.println(list.lastIndexOf(5));
        System.out.println(list.subList(1, 4));
        //list.subList(1,5).forEach(System.out::println);

        //常用遍历方法
        System.out.println("=========List常用遍历方法==========");
        //1
        System.out.println(list);
        System.out.println("===================");
        //2
        for (int i = 0; i < list.size(); i++) {
            System.out.print(list.get(i)+" ");
        }
        System.out.println();
        System.out.println("==================");
        //3
        for(Integer x:list){
            System.out.print(x+" ");
        }
        System.out.println();
        System.out.println("==================");
        //4  Iterator遍历
        Iterator<Integer> it = list.iterator();
        while(it.hasNext()){
            System.out.print(it.next()+" ");
        }
        System.out.println();
        System.out.println("==================");
        //5   ListIterator遍历，继承自Iterator
        ListIterator<Integer> it2=list.listIterator();
        while(it2.hasNext()){
            System.out.print(it2.next()+" ");
        }
        System.out.println();
        System.out.println("====================");
        //6   ListIterator逆序遍历
        ListIterator<Integer> it3=list.listIterator(list.size());
        while(it3.hasPrevious()){
            System.out.print(it3.previous()+" ");
        }
        System.out.println();
    }
}
